#ifndef CORE_BVH_H
#define CORE_BVH_H
#include <iostream>
#include "Hitable.h"
#include "HitableList.h"
namespace RayTracer
{

	class BvhNode : public Hitable {
	public:
		BvhNode();

		BvhNode(const HitableList& list, float time0, float time1)
			: BvhNode(list.objects, 0, list.objects.size(), time0, time1){}

		BvhNode(
			const std::vector<std::shared_ptr<Hitable>>& src_objects,
			size_t start, size_t end, float time0, float time1);

		bool Hit(const Ray& r, float t_min, float t_max, HitRecord& rec) const ;

		bool BoundingBox(AABB& output_box) const ;

	private:
		bool box_compare(const std::shared_ptr<Hitable> a, const std::shared_ptr<Hitable> b, int axis) {
			AABB box_a;
			AABB box_b;

			if (!a->BoundingBox(box_a) || !b->BoundingBox(box_b))
				std::cout << "No bounding box in bvh_node constructor.\n";

			return box_a.Min()[axis] < box_b.Min()[axis];
		}


		bool box_x_compare(const std::shared_ptr<Hitable> a, const std::shared_ptr<Hitable> b) {
			return box_compare(a, b, 0);
		}

		bool box_y_compare(const std::shared_ptr<Hitable> a, const std::shared_ptr<Hitable> b) {
			return box_compare(a, b, 1);
		}

		bool box_z_compare(const std::shared_ptr<Hitable> a, const std::shared_ptr<Hitable> b) {
			return box_compare(a, b, 2);
		}

	public:
		std::shared_ptr<Hitable> left;
		std::shared_ptr<Hitable> right;
		AABB box;
	};

	
}

#endif